package com.evamay.kotlindemo.ui.viewpager

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.evamay.kotlindemo.R
import kotlinx.android.synthetic.main.activity_view_pager.*

class ViewPagerActivity :AppCompatActivity() {
    var type=0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager)
        initData()
    }

    private fun initData() {
        type=intent.getIntExtra("type",0)
        if (type != 7) {
            var mAdapter = PagerAdapter(this)
            view_pager.adapter = mAdapter
        } else {
            var mAdapter=FragmentAndPagerAdapter(this)
            view_pager.adapter=mAdapter
        }
        view_pager.offscreenPageLimit=4
        when(type){
            1->{
                //默认状态
            }
            2->{
                //纵向滑动
                view_pager.orientation=ViewPager2.ORIENTATION_VERTICAL
            }
            3->{
                //禁用滑动
                view_pager.isUserInputEnabled=false
                tv_next.visibility=View.VISIBLE
                tv_next.setOnClickListener{
                    view_pager.beginFakeDrag()//开启模拟拖拽
                    //fakeDragBy()返回true表示有fakeDrag在执行，返回false表示当前没有fakeDrag在执行，参数为正往上一个页面滑动，为负往下一个页面滑动
                    if (view_pager.fakeDragBy(-110f)) {
                        view_pager.endFakeDrag()
                    }
                }
            }
            4->{
                //设置滑动间距
                view_pager.setPageTransformer(
                        MarginPageTransformer(16)
                )
            }
            5->{
                //添加组合动画
                var compositePageTransformer=CompositePageTransformer()
                compositePageTransformer.addTransformer(MarginPageTransformer(16))
                compositePageTransformer.addTransformer(ScaleInTransformer())
                view_pager.setPageTransformer(compositePageTransformer)
            }
            6->{
                //设置一屏多页
                view_pager.apply {
                    offscreenPageLimit=1
                    val recyclerView=getChildAt(0) as RecyclerView
                    recyclerView.apply {
                        val padding=resources.getDimensionPixelOffset(R.dimen.dp_10)+resources.getDimensionPixelOffset(R.dimen.dp_10)
                        setPadding(padding,0,padding,0)
                        clipToPadding=false
                    }
                }
                val compositePageTransformer=CompositePageTransformer()
                compositePageTransformer.addTransformer(ScaleInTransformer())
                compositePageTransformer.addTransformer(MarginPageTransformer(10))
                view_pager.setPageTransformer(compositePageTransformer)
            }
        }
    }
}